<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
 */

namespace App\Controller\Api;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Request;
use Hyperf\View\RenderInterface;
use Illuminate\Support\Facades\Hash;
define('VISITOR_CARD_PUB_KEY','./fccsplatform_usercard__1Az1gjY-pub-key_36863.pem');

class CommunityController{


    public function customerservice(Request $request){

        // 替换这里的tntInstId和scene的参数值
        //
        // 1. 金融云租户, 支付宝小程序租户, 钉钉租户
        // $baseUrl = 'https://cschat.antcloud.com.cn/index.htm?tntInstId=xxxx&scene=xxxx';
        // 2. 阿里云租户
        $baseUrl = 'https://cschat-ccs.aliyun.com/index.htm?tntInstId=_1Az1gjY&scene=SCE00006432';

        //生成随即16位AES密钥
        $aesKey = md5(time(), true);

      	if(empty($request->userId)||empty($request->uname)){

        	$result['status'] = false;
            $result['statusCode'] = 400;
            $result['message'] = '传参失败';
          	$result['data'] = (object)array();

          	return json_encode($result,1);
        }


        //需要根据实际系统用户的信息修改
        $cInfoArray = [
            'extInfo' => [
            'phone'=>'34534535',
            ],
            'userId' => $request->userId,
            'uname' => $request->uname,

        ];

        $jsonStr = json_encode($cInfoArray, 1);

        $cInfo = self::AESEncrypt($jsonStr, $aesKey);



        //RSA 公钥加密aesKey
        openssl_public_encrypt($aesKey, $encrypted, file_get_contents(VISITOR_CARD_PUB_KEY));

        //构造带有访客信息的加密链接
      	$result['status'] = true;
        $result['statusCode'] = 200;
        $result['message'] = '成功';
        $result['data']['url'] =  $baseUrl . '&cinfo=' . urlencode(base64_encode($cInfo)) . '&key=' . urlencode(base64_encode($encrypted));
        return json_encode($result,1);
		//return $result['data']['url'];



    }


  	    //页面嵌入 访客名片
    public function actionIframeVisitorCard()
    {
        $params = $_GET['params'];
        $key = $_GET['key'];

        //RSA 公钥解密
        openssl_public_decrypt(base64_decode($key), $decrypted, file_get_contents(VISITOR_CARD_PUB_KEY));

        //AES解密 params
        $aesParams = self::AESDecrypt($params, $decrypted);

        //json数据转为数组
        $res = json_decode($aesParams, 1);


      	 $info = DB::table('members')->select('id','name','mobile')->where('id',$res['userId'])->first();

      	if(empty($info)){

        	echo '<h3 style="color:red">非法或测试用户</h3><br>';
        }else{

        $data['message'] = '查询成功';
      	$data['success'] = true;
		$data['result']['userId'] = $res['userId'];
      	$data['result']['userName'] = $info->name;
      	$data['result']['telePhone'] = $info->mobile;
      	$data['result']['schema']['properties']['customerName']['name'] = $info->name;
      	$data['result']['schema']['properties']['customerName']['type'] = 'text';
      	 //var_dump($data);
        echo '<h3>用户名</h3>'.$data['result']['userName'].'<br>';
        echo '<h3>用户的userId</h3>'.$data['result']['userId'].'<br>';
   		echo '<h3>用户的电话</h3>'.$data['result']['telePhone'].'<br>';
        }

    }






            /**
     * AES-128-ECB 加密算法
     * @param String $str 需要加密字符串
     * @param String $key 密钥
     * @return string
     */
    private static function AESEncrypt($str, $key)
    {
        return openssl_encrypt($str, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
    }

    /**
     * AES-128-ECB 解密算法
     * @param String $strEncode base64Encode之后的加密串
     * @param String $key 密钥
     * @return string
     */
    private static function AESDecrypt($strEncode, $key)
    {
        return openssl_decrypt(base64_decode($strEncode), 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
    }



}


?>